package com.hzj.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzj.pojo.Dict;
import com.hzj.service.DictService;
import com.hzj.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/cmn/dict")
public class ApiCmnController {
    @Autowired
    DictService ds;

    @ApiOperation(value = "查询数据并判断是否有子类")
    @RequestMapping("/chiefAll/{id}")
    public Result chiefAll(@PathVariable int id) {
        List<Dict> childDict = ds.findChildDict(id);
        return Result.ok(childDict);
    }

    //查询所以省份
    @PostMapping("/findByDictCode/{dictCode}")
    @Cacheable(cacheNames = "findByDictCode", key = "#dictCode")
    public Result findByDictCode(@PathVariable String dictCode) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dict_code", dictCode);
        List<Dict> one = ds.list(wrapper);
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("parent_id", one.get(0).getParentId());
        List list = ds.list(wrapper1);

        return Result.ok(list);
    }

    //根据省份id查询市
    @PostMapping("/findChildData/{parentId}")
    @Cacheable(cacheNames = "findChildData", key = "#parentId")
    public Result findChildData(@PathVariable int parentId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id", parentId);
        List list = ds.list(wrapper);
        return Result.ok(list);
    }
}
